package com.atlin.project.controller;

import com.atlin.project.annotation.AuthCheck;
import com.atlin.project.common.BaseResponse;
import com.atlin.project.common.DeleteRequest;
import com.atlin.project.common.ErrorCode;
import com.atlin.project.common.ResultUtils;
import com.atlin.project.contant.UserConstant;
import com.atlin.project.exception.BusinessException;
import com.atlin.project.exception.ThrowUtils;
import com.atlin.project.model.domain.LoginInfo;
import com.atlin.project.model.dto.loginInfo.LoginInfoQueryRequest;
import com.atlin.project.service.LoginInfoService;
import com.atlin.project.service.UserService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.gson.Gson;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * 登录日志
 *
 * @author atlin
 */
@RestController
@RequestMapping("/loginInfo")
public class LoginLogController {

    @Resource
    private LoginInfoService loginInfoService;

    @Resource
    private UserService userService;

    private final static Gson GSON = new Gson();

    // region 增删改查

    /**
     * 删除
     *
     * @param deleteRequest
     * @param request
     * @return
     */
    @PostMapping("/delete")
    @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
    public BaseResponse<Boolean> deleteLoginInfo(@RequestBody DeleteRequest deleteRequest, HttpServletRequest request) {
        if (deleteRequest == null || deleteRequest.getId() <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        long id = deleteRequest.getId();
        // 判断是否存在
        LoginInfo oldLoginInfo = loginInfoService.getById(id);
        ThrowUtils.throwIf(oldLoginInfo == null, ErrorCode.NULL_ERROR);
        
        boolean b = loginInfoService.removeById(id);
        return ResultUtils.success(b);
    }

    
    /**
     * 根据 id 获取
     *
     * @param id
     * @return
     */
    @GetMapping("/get")
    @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
    public BaseResponse<LoginInfo> getLoginInfoById(long id, HttpServletRequest request) {
        if (id <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        LoginInfo loginInfo = loginInfoService.getById(id);
        if (loginInfo == null) {
            throw new BusinessException(ErrorCode.NULL_ERROR);
        }
        return ResultUtils.success(loginInfo);
    }
    

    /**
     * 分页获取题目列表（仅管理员）
     *
     * @param loginInfoQueryRequest
     * @param request
     * @return
     */
    @PostMapping("/list/page")
    @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
    public BaseResponse<Page<LoginInfo>> listLoginInfoByPage(@RequestBody LoginInfoQueryRequest loginInfoQueryRequest,
                                                           HttpServletRequest request) {
        long current = loginInfoQueryRequest.getCurrent();
        long size = loginInfoQueryRequest.getPageSize();
        Page<LoginInfo> loginInfoPage = loginInfoService.page(new Page<>(current, size),
                loginInfoService.getQueryWrapper(loginInfoQueryRequest));
        return ResultUtils.success(loginInfoPage);
    }

    // endregion


}
